<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="perm" uri="/WEB-INF/tld/perm.tld"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<script>
	var ctx = "${ctx}";
</script>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品管理</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="Cache-Control" content="no-cache, must-revalidate">
	<meta http-equiv="expires" content="Wed, 26 Feb 209908:21:57 GMT"> 
   
	<link href="${ctx}/static/css/bootstrap.min.css?v=${static_version}" rel="stylesheet" />
	<link href="${ctx}/static/css/font-awesome.min.css?v=4.4.0?v=${static_version}" rel="stylesheet" />
	<link href="${ctx}/static/css/animate.min.css?v=${static_version}" rel="stylesheet" />
	<link href="${ctx}/static/css/style.min.css?v=${static_version}" rel="stylesheet" />
	<link href="${ctx}/static/css/common.css?v=${static_version}" rel="stylesheet" />
	<link href="//at.alicdn.com/t/font_9x03jhokyws9vn29.css" rel="stylesheet">
	<!-- Data Tables -->
	<link href="${ctx}/static/css/plugins/dataTables/dataTables.bootstrap.css?v=${static_version}" rel="stylesheet" />
    
</head>
<body class="gray-bg">
	<div class="wrapper wrapper-content  animated fadeInRight">
			<div class="ibox float-e-margins">
								
        		<!-- 商品列表 -->
                <div class="ibox-title clearfix">
                    <h5>商品列表<small></small></h5>
                      <div  style="float:right">
							<button name="" id="" class="btn btn-primary radius" onclick="javascript:location.replace(location.href);" type="submit">刷新</button>	
		</div>
                </div>
                
				<div class="ibox-content">
				
					<!-- 查询 -->
					<div class="row  m-b-xs">
	                <form  action="${ctx}/seller/goods-search" method="get" class="form-group" id="goods_table_form">	
	                	<input type="hidden" name="orderBy" value="create_date desc">
	                	<input type="hidden" id="brand-select-id" name="brandId" value="">
	                	<input type="hidden" id="cat-select-id" name="catId" value="">
	                	<input type="hidden" id="isZiYing" name="isZiYing" value="${isZiYing}">  
	
	                        <div class="col-sm-3">
	                        	<div class="col-sm-3 m-t-xs">名称</div>
	                        	<div class="col-sm-9"><input id="name" name="name" type="text" placeholder="请输入名称或者编号" class="form-control" /></div>
	                        </div>
	                        <input type="hidden" name="type" id="endType" value="${conditions.type}">  
	                        <div class="col-sm-1 m-t-xs">分类</div>         
							<div class="col-md-2 col-sm-2">
								<select data-placeholder="选择大类" id="type1" name="type1" onchange="firstChange(this);" class="input-md form-control chosen-select" tabindex="4">
				                    <option value="">选择大类</option>
				                    <c:forEach items="${types1}" var="type">
											<option value="${type.cCode}"
												<c:if test="${type.cCode==conditions.type1}">selected</c:if>>${type.cName}</option>
										</c:forEach>
				                </select>
							</div>				
							<div class="col-md-2 col-sm-2">
								<select data-placeholder="选择中类" id="type2" name="type2" onchange="secondChange(this);" onclick="secondClick(this)" class="input-md form-control chosen-select" tabindex="4">
				                    <option value="">选择中类</option>
				                    	<c:forEach items="${types2}" var="type">
											<option value="${type.cCode}"
												<c:if test="${type.cCode==conditions.type2}">selected</c:if>>${type.cName}</option>
										</c:forEach>
				                </select>
							</div>					
							<div class="col-md-2 col-sm-2">
								<select data-placeholder="选择小类" id="type3"  name="type3" onclick="thirdClick(this)" class="input-md form-control chosen-select" tabindex="4">
				                    <option value="">选择小类</option>
				                    	<c:forEach items="${types3}" var="type">
											<option value="${type.cCode}"
												<c:if test="${type.cCode==conditions.type3}">selected</c:if>>${type.cName}</option>
										</c:forEach>
				                </select>
							</div>
							        
							<div class="col-md-1 col-sm-1">
								<button name="" id="" class="btn btn-primary radius" type="submit">查询</button>								
							</div>
						
						</form>	    
					</div>
					<!-- /查询 -->
					
					<div class="hr-line-dashed3"></div>
					
					<!-- 批量操作-->
					<div class="row">
						<input type="hidden" id="id" name="id" value='${goods.id}' />
	
					    <div class="col-md-1 ">
					   		 <button class="btn btn-primary radius" id="batchBtn" onclick="batchOutGoods(2);">批量下架</button>
						</div>
						<div class="col-md-6 ">
							<button class="btn btn-primary radius" id="batchBtn2" onclick="batchOutGoods(1);">批量上架</button>
					  	</div>
											
					    <div class="col-md-1">
							<a class="btn btn-primary" onclick="goods_add('添加产品','${ctx}/seller/goods-add-pre?isZiYing=${isZiYing}')" href="javascript:;">添加</a>
						</div>
						<div class="col-md-1">
							<button name="" onclick="theReset();" id="" class="btn btn-warning btn-outline radius">重置</button>
						</div>	
                   </div>
     			   <!-- /批量操作-->
     			   
     			   <div class="hr-line-dashed3"></div>
     			   	
				<table id="goods_table" data-action="" data-datatable="true"  class="table table-striped table-bordered table-hover dataTables-example">
				  	<thead>
					  	<tr>	
					  	<th width="25" class="ckAll"><input type="checkbox" class="box-all" name="" value=""></th>	
					  		<th class="text-center" width="40">ID</th>
					<th class="text-center">名称</th>
					<th class="text-center">缩略图</th>
					<th class="text-center">所属分类</th>
					<th class="text-center" width="40">排序</th>
					<th class="text-center" width="80">产地</th>
					<!-- <th width="80">已售数量</th> -->
					<th class="text-center" >所在县市</th>
					<th class="text-center" >所属商家</th>
					<th class="text-center" >是否删除</th>
					<th class="text-center" >状态</th>
					<th class="text-center">操作</th>
					  	</tr>
				  	</thead>
				  	<tbody>
				  	<c:forEach items="${goods_list}" var="goods">
					<tr class="text-c">
						<td><input type="checkbox" class="box-items" value="${goods.id}"
							name="goodsIds"><input  type="hidden" id="id" name="id" value='${goods.id}' /></td>
						<td class="text-center">${goods.id}</td>
						<td>${goods.name}</td>
						<td class="text-center"><img src="${goods.image}" height="30" border="0" /></td>
						<td class="text-center"><c:forEach items="${types}" var="type">
								<c:if test="${type.cCode==goods.type}">
									${type.cName}
								</c:if>
							</c:forEach></td>
						<td class="text-center">${goods.sort}</td>
						<td class="text-center">${goods.producing_area}</td>
						<%-- <td class="text-center">${goods.count}</td> --%>
						<td class="text-center"><c:forEach items="${citys}" var="city">
								<c:if test="${city.id==goods.city_id}">
									${city.name}
								</c:if>
							</c:forEach></td>

						<td class="text-center"><c:forEach items="${shops}" var="shop">
								<c:if test="${shop.cId==goods.shop_id}">
										${shop.cSname}
									</c:if>
							</c:forEach></td>
						<td class="text-center"><c:choose>
								<c:when test="${1!=goods.is_deleted}">
									<span class="btn btn-primary-line  btn-xs btn-line">有效</span>
								</c:when>
								<c:otherwise>
									<span class="btn btn-default-line btn-xs btn-line">已删除</span>
								</c:otherwise>
							</c:choose></td>
						<td class="text-center"><c:choose>
								<c:when test="${goods.status==1}">
									<span class="btn btn-primary-line  btn-xs btn-line">出售中</span>
								</c:when>
								<c:when test="${goods.status==2}">
									<span class="btn btn-default-line btn-xs btn-line">已下架</span>
								</c:when>
								<c:when test="${goods.status==0}">
									<span class="btn btn-warning-line btn-xs btn-line">未发布</span>
								</c:when>
								<c:otherwise>
									<span class="btn btn-default-line btn-xs btn-line">未知</span>
								</c:otherwise>
							</c:choose></td>
						<td class="td-manage text-center">
							<a class="btn btn-primary radius"
							onClick="goods_edit(this,'${goods.id}')" href="javascript:;">编辑</a> 
							<c:if
								test="${1 != goods.is_deleted}">
								<a class="btn btn-primary radius" onClick="goods_disable(this,'${goods.id}')" href="javascript:;">删除</a>
							</c:if>
						</td>
					</tr>
				</c:forEach>
				  	</tbody>
			  	</table>
			  	
				</div>
		 </div>
</div>

		
	<script src="${ctx}/static/js/jquery.min.js?v=${static_version}"></script>
	<script src="${ctx}/static/js/bootstrap.min.js?v=${static_version}"></script>
	<script src="${ctx}/static/js/plugins/layer/layer.js?v=${static_version}"></script>
	<!-- 插件脚本 -->
	<script
		src="${ctx}/static/js/plugins/dataTables/jquery.dataTables.js?v=${static_version}"></script>
	<script
		src="${ctx}/static/js/plugins/dataTables/dataTables.bootstrap.js?v=${static_version}"></script>
	
	<script type="text/javascript">
	$(document).ready(function() {
			$(".dataTables-example").dataTable();
		});
	
	$(function(){
$('.ckAll').click(function(){
$(".box-items").each(function(){
  $(this).prop("checked",!!$(".box-all").prop("checked"));
});
});
});
	
	$("#goods_table_search").click(function(){
		var name=$("#name").val();
		var type=$("#endType").val();
		var type1=$("#type1").val();
		var type2=$("#type2").val();
		var type3=$("#type3").val();
		$.ajax({
			type : "POST",
			url : "${ctx}/seller/goods-search",
			async:false,
			data : {name:name,type1:type1,type2:type2,type3:type3,type:type},
			error : function() {
				layer.msg('操作失败', {icon:5,time:1000});
			},
			success : function(msg) {
				
			}
		})
	})
		/*
 		批量下架商品
	*/	
	function batchOutGoods(status){

		var goodsIds = $("input[name='goodsIds']:checked");
		if (goodsIds.length <= 0){
			var msg = '';
			if (status == 2){
				msg = '请勾选要下架的商品';
				layer.tips(msg, '#batchBtn');
			}
			if (status == 1){
				msg = '请勾选要上架的商品';
				layer.tips(msg, '#batchBtn2');
			}
			return ;
		}
		var data = "";
		for (var i = 0; i < goodsIds.length; i++){
			/* if (i == 0){
				data += "goodsIds=" + goodsIds[i].value; 
			}else{
				data += "&goodsIds=" + goodsIds[i].value; 
			} */
			data+=goodsIds[i].value+",";
		}
		//data += "&status="+status;
		data=data.substring(0,data.length-1);
		$.ajax({
					type : "POST",
					url :"${ctx}/seller/goodsBatchOut",
					async:false,
					data : {goodsIds:data,status:status},
					error : function() {
						layer.msg('操作失败', {icon:5,time:1000});
					},
					success : function(msg) {
						layer.msg('操作成功',{icon:6,time:1000});
						$(".btn-refresh").click();
						location.replace(location.href);
					}
					
				});
		/* $.post("${ctx}/seller/goods-batch-out", data)
		.done(function(data){
			if (data.error == "0000"){
				layer.msg(data.information,{icon:6});
				$(".btn-refresh").click();
			}else{
				layer.msg(data.information,{icon:5});
			}
		}) */
	}
		/*
		重置
		*/
		function theReset(){
			$("#search-form input[type='text']").val('');
			$("#search-form select").val(-1);
			$("#endType").val("-1");
		}
		/* /*添加*/
		function goods_add(title,url){
			var index = layer.open({
				type: 2,
				title: title,
				content: url
			});
			layer.full(index);
		} 
		/*编辑*/
		function goods_edit(obj,id,w,h){
			isZiYing = $("#isZiYing").val();
			var edit_page = "${ctx}/seller/goods-add-pre?isZiYing="+isZiYing+"&id="+id;
			var index = layer.open({
				type: 2,
				title: "编辑商品",
				content: edit_page
			});
			layer.full(index);
			//layer_show("编辑商品",edit_page,w,h);
		}
		/*查看*/
		function goods_show(title,url,id){
			var index = layer.open({
				type: 2,
				title: title,
				content: url
			});
			layer.full(index);
		}
	/*删除*/
		function goods_disable(obj,id){
			layer.confirm('确认删除？', {
				btn: ['确认','取消'], 
				shade: false
			},
			function(){
				$.ajax({
					type : "POST",
					url :"${ctx}/seller/goods-disable",
					data : "id=" + id,
					error : function() {
						layer.msg('操作失败', {icon:5,time:1000});
					},
					success : function(msg) {
					location.href ="${ctx}/seller/goods-search"; 
					}
				});
			},
			function(){
				layer.msg('未删除', {icon:5,time:1000});
			});	
		}
		var type1;
var type2;
var type3;		
function firstChange(theSelect){
	var typeId = theSelect.value;

	if (!type2){
		$.ajax({
			type:"post",
			async:false,
			url:"${ctx}/seller/get-types",
			data : {level:2,isZiYing:true},
			success : function(msg){
				type2 = msg;
			}
			});
		}
	var options = "<option value='-1'>--中类--</option>";
	for (var i = 0; i < type2.length; i++){
		if (typeId == type2[i].cBelong){
			var aOption = "<option value='"+type2[i].cCode+"' >"+type2[i].cName+"</option>"
			options += aOption; 
		}
	}
	$("#type2").html(options);
}

function secondClick(theSelect){
	
	var typeId = $("#type1").val();
	if (!type2){
		$.ajax({
			type:"post",
			async:false,
			url:"${ctx}/seller/get-types",
			data : {level:2,isZiYing:true},
			success : function(msg){
				type2 = msg;
			}
			});
		
		var options = "<option value='-1'>--中类--</option>";
		for (var i = 0; i < type2.length; i++){
			if (typeId == type2[i].cBelong){
				var aOption = "<option value='"+type2[i].cCode+"' >"+type2[i].cName+"</option>"
				options += aOption; 
			}
		}
		$("#type2").html(options);
		}
}

function secondChange(theSelect){
	var typeId = theSelect.value;

	if (!type3){
		$.ajax({
			type:"post",
			async:false,
			url:"${ctx}/seller/get-types",
			data : {level:3,isZiYing:true},
			success : function(msg){
				type3 = msg;
			}
			});
		}
	var options = "<option value='-1'>--小类--</option>";
	for (var i = 0; i < type3.length; i++){
		if (typeId == type3[i].cBelong){
			var aOption = "<option value='"+type3[i].cCode+"' >"+type3[i].cName+"</option>"
			options += aOption; 
		}
	}
	$("#type3").html(options);
}

function thirdClick(theSelect){
	
	var typeId = $("#type2").val();
	if (!type3 && typeId != -1){
		$.ajax({
			type:"post",
			async:false,
			url:"${ctx}/seller/get-types",
			data : {level:3,isZiYing:true},
			success : function(msg){
				type3 = msg;
			}
			});
		var options = "<option value='-1'>--小类--</option>";
		for (var i = 0; i < type3.length; i++){
			if (typeId == type3[i].cBelong){
				var aOption = "<option value='"+type3[i].cCode+"' >"+type3[i].cName+"</option>"
				options += aOption; 
			}
		}
		$("#type3").html(options);
		}
	
}
	</script>
	</body>
</html>